package com.kkb.shopcenter.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.shopcenter.api.BusinessService;
import com.kkb.shopcenter.common.common.HttpResult;
import com.kkb.shopcenter.common.exception.DefinitionException;
import com.kkb.shopcenter.common.pojo.BusinessInfo;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
public class BusinessController {


    @Reference
    private BusinessService businessService;

    //商户列表
    @ResponseBody
    @GetMapping("/getBusinessList")
    public HttpResult getBusinessList(@RequestParam(value="page",defaultValue="1")int pageNo, @RequestParam(value="pageSize",defaultValue="10")int pageSize) {
        PageInfo<BusinessInfo> businessList = businessService.getBusinessList(pageNo+1,pageSize,-1);
        return HttpResult.success(businessList);
    }

    //商家审核
    @ResponseBody
    @GetMapping("/businessReview")
    public HttpResult businessReview(@RequestParam(value="page",defaultValue="1")int pageNo, @RequestParam(value="pageSize",defaultValue="10")int pageSize) {
        PageInfo<BusinessInfo> businessList = businessService.getBusinessList(pageNo+1,pageSize,0);
        return HttpResult.success(businessList);
    }

    @ResponseBody
    @PostMapping("/register")
    public HttpResult register(@RequestBody BusinessInfo businessInfo){
        if(StringUtils.isEmpty(businessInfo.getCompName())||StringUtils.isEmpty(businessInfo.getShopInfo().getShopName())
    ||StringUtils.isEmpty(businessInfo.getUserInfo().getUserName())){
            return HttpResult.defineError(new DefinitionException("请输入必填项"));
        }
        int compNameExist = businessService.selectCompNameExit(businessInfo.getCompName());
        if(compNameExist>0){
            return HttpResult.defineError(new DefinitionException("该商家名称已配占用"));
        }

        int shopNameExist = businessService.shopNameIsExist(businessInfo.getShopInfo().getShopName());
        if(shopNameExist>0){
            return HttpResult.defineError(new DefinitionException("该店铺名称已配占用"));
        }

        int userNameExist = businessService.shopUserNameIsExist(businessInfo.getUserInfo().getUserName());
        if(userNameExist>0){
            return HttpResult.defineError(new DefinitionException("该用户名称已配占用"));
        }

        int result =   businessService.register(businessInfo);

        return HttpResult.success(result);
    }
}
